package ch.exm.storm.query.hibernate;

import java.util.Collection;

import org.hibernate.Query;
import org.hibernate.Session;

import ch.exm.storm.log.Logger;
import ch.exm.storm.query.relational.AbstractRelationalQueryPlan;

public class HibernateQueryPlan extends AbstractRelationalQueryPlan {
	
	private SessionProvider sessionProvider;

	HibernateQueryPlan(SessionProvider sessionProvider) {
		this.sessionProvider = sessionProvider;
	}
	
	public Collection<?> execute() {
		Session session = sessionProvider.provideSession();

		String queryStatement = getRelationalStatement();
		Logger.debug(this, "Performing hql query: " + queryStatement);
		Query query = session.createQuery(getRelationalStatement());
		
		Collection<?> result = query.list();
		result = applyPostConditions(result);
		result = applyPostSelections(result);
			
		sessionProvider.disposeSession(session);
		
		return result;
	}
}
